package org.bai.algorithm.day02;

import org.bai.algorithm.entity.ListNode;
import org.bai.algorithm.util.ListNodeUtil;
import org.bai.algorithm.util.RevealUtil;

/**
 * @ClassName LinkedListReversal
 * @Description <a href="https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof/">链表反转</a>
 * @Author bai
 * @Date 2021/8/10 23:57
 * @Version 1.0
 **/
public class LinkedListReversal {

    public static ListNode<Integer> reverseList(ListNode<Integer> head) {
        ListNode<Integer> temporaryHeadNode = new ListNode<>();
        ListNode<Integer> temporaryNode;

        while (head != null) {
            temporaryNode = head;
            head = head.next;

            temporaryNode.next = temporaryHeadNode.next;
            temporaryHeadNode.next = temporaryNode;
        }

        return temporaryHeadNode.next;
    }

    public static void main(String[] args) {
        ListNode<Integer> headNode = ListNodeUtil.buildInt(10, 100);
        RevealUtil.reveal(headNode);
        RevealUtil.reveal(reverseList(headNode));
    }

}
